package gui.item;

import java.util.*;

import core.Item;

import gui.common.Tagable;

/**
 * Display data class for items.
 */
public class ItemData extends Tagable implements Comparable<ItemData> {

	/**
	 * EntryDate attribute.
	 */
	private Date _entryDate;
	
	/**
	 * ExpirationDate attribute.
	 */
	private Date _expirationDate;
	
	/**
	 * Barcode attribute.
	 */
	private String _barcode;
	
	/**
	 * StorageUnit attribute.
	 */
	private String _storageUnit;
	
	/**
	 * ProductGroup attribute.
	 */
	private String _productGroup;

	/**
	 * Constructor.
	 * 
	 * {@pre None}
	 * 
	 * {@post getEntryDate() == current date/time}
	 * {@post getExpirationDate() == current date/time}
	 * {@post getBarcode() == ""}
	 * {@post getStorageUnit() == ""}
	 * {@post getProductGroup() == ""}
	 */
	public ItemData() {
		_entryDate = new Date();
		_expirationDate = new Date();
		_barcode = "";
		_storageUnit = "";
		_productGroup = "";
	}
	
	/**
	 * Constructor
	 * converts an Object to ItemData
	 */
	public ItemData(Object o)
	{
		if (o == null)
			throw new IllegalArgumentException("Object cannot be null");

		this.setTag(o);
		reloadItemInfo();
	}
	
	public void reloadItemInfo(){
		Item item = getItem();
		
		_entryDate = item.getEntryDate().getTime();
		_expirationDate = item.getExpirationDate().getTime();
		//there should be no expiration date if the shelf life is zero
		if(_entryDate.equals(_expirationDate)){
			_expirationDate = null;
		}
		
		_barcode = item.getBarcodeString();
		if (item.getContainer() != null){
			_storageUnit = item.getContainer().getStorageUnit().getName();
			
			if (!_storageUnit.equals(item.getContainer().getName()))
				_productGroup = item.getContainer().getName();
			else
				_productGroup = "";
		}
		else{
			_productGroup = "";
			_storageUnit = "";
		}		
	}
	
	
	/**
	 * Returns the value of the Barcode attribute.
	 */
	public String getBarcode() {
		return _barcode;
	}

	/**
	 * Sets the value of the Barcode attribute.
	 * 
	 * @param barcode New Barcode value
	 * 
	 * {@pre barcode != null}
	 * 
	 * {@post getBarcode() == barcode}
	 */
	public void setBarcode(String barcode) {
		this._barcode = barcode;
	}

	/**
	 * Returns the value of the EntryDate attribute.
	 */
	public Date getEntryDate() {
		return _entryDate;
	}

	/**
	 * Sets the value of the EntryDate attribute.
	 * 
	 * @param entryDate New EntryDate value
	 * 
	 * {@pre entryDate != null}
	 * 
	 * {@post getEntryDate() == entryDate}
	 */
	public void setEntryDate(Date entryDate) {
		this._entryDate = entryDate;
	}

	/**
	 * Returns the value of the ExpirationDate attribute.
	 */
	public Date getExpirationDate() {
		return _expirationDate;
	}

	/**
	 * Sets the value of the ExpirationDate attribute.
	 * 
	 * @param expirationDate New ExpirationDate value
	 * 
	 * {@pre None}
	 * 
	 * {@post getExpirationDate() == expirationDate}
	 */
	public void setExpirationDate(Date expirationDate) {
		this._expirationDate = expirationDate;
	}

	/**
	 * Returns the value of the StorageUnit attribute.
	 */
	public String getStorageUnit() {
		return _storageUnit;
	}

	/**
	 * Sets the value of the StorageUnit attribute.
	 * 
	 * @param storageUnit New StorageUnit value
	 * 
	 * {@pre storageUnit != null}
	 * 
	 * {@post getStorageUnit() == storageUnit}
	 */
	public void setStorageUnit(String storageUnit) {
		this._storageUnit = storageUnit;
	}

	/**
	 * Returns the value of the ProductGroup attribute.
	 */
	public String getProductGroup() {
		return _productGroup;
	}

	/**
	 * Sets the value of the ProductGroup attribute.
	 * 
	 * @param productGroup New ProductGroup value
	 * 
	 * {@pre productGroup != null}
	 * 
	 * {@post getProductGroup() == productGroup}
	 */
	public void setProductGroup(String productGroup) {
		this._productGroup = productGroup;
	}

	/**
	 * Gets the Item associated with this ItemData
	 * 
	 * @return the associated Item, returns null if no item
	 */
	public Item getItem()
	{
		return (Item)(this.getTag());
	}

	@Override
	public int compareTo(ItemData arg0) {
		return _entryDate.compareTo(arg0._entryDate);
	}
}

